<?php
namespace App\Http\Controllers\Xcx\Ywuser;

use App\Service\Xcx\Ywuser\TmpService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class TmpController extends BaseController
{
    //个人码

    //列表
    public function index(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 6);
        $pas['uid'] = $request->post('uid', 0);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['shequ_id'] = $request->post('shequ_id', 0);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $res = TmpService::index($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function one(Request $request)
    {
        $code = $request->post('code', '');
        $res = TmpService::one($code);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function onebyid(Request $request)
    {
        $id = $request->post('id', 0);
        $res = TmpService::onebyid($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function onebyserial(Request $request)
    {
        $serial = $request->get('show_code', '');
        $res = TmpService::codeBySerial($serial);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $type = $res['data']['yw_type'];
        $id = $res['data']['yw_id'];
        if ($type != 1) {
            return $this->toJson(-2, '类型错误');
        }
        $res = TmpService::onebyid($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //新增
    public function add(Request $request)
    {
        $pas['uid'] = $request->post('uid', 0);
        $pas['code'] = $request->post('code', '');
        $pas['thumb'] = $request->post('thumb', '');
        $pas['title'] = $request->post('title', '');
        $pas['describe'] = $request->post('detail', '');
        $shequ_name = $request->post('shequ_name', '');
        $pas['cate_id'] = $request->post('cate_id', 0);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['code']) {
            return $this->toJson(-2, '码必须的');
        }
        if (!$pas['thumb']) {
            return $this->toJson(-2, '图片必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '标题必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '分类必须的');
        }
        if (strpos($pas['thumb'], '.top/')) {
            $thumbs = explode('.top/', $pas['thumb']);
            $pas['thumb'] = $thumbs[1];
        }
        $res = TmpService::add($pas, $shequ_name);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //分类列表
    public function cateList()
    {
        $res = TmpService::cateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //分类设置
    public function cateEdit(Request $request)
    {
        $tmp_id = $request->post('id', 0);
        $cate_id = $request->post('cate_id', 0);
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if (!$cate_id) {
            return $this->toJson(-2, '分类必须的');
        }
        $res = TmpService::cateEdit($tmp_id, $cate_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('tmpid', 0);
        $pas['title'] = $request->post('title', '');
        $pas['describe'] = $request->post('detail', '');
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = TmpService::modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //图片上传
    public function imgUpload(Request $request)
    {
        $id = $request->post('tmpid', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $resUpload = Upload::uploadImg($request, 'img');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        //$img = env('DOMAIN') . '/' .$resUpload['data'];
        $img = $resUpload['data'];
        $res = TmpService::imgUpload($id, $img);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //属性列表
    public function attrList(Request $request)
    {
        $cate_id = $request->post('cate_id', 0);
        $tmp_id = $request->post('id', 0);
//        if (!$cate_id) {
//            return $this->toJson(-2, '分类必须的');
//        }
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = TmpService::attrList($cate_id, $tmp_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //属性修改
    public function attrEdit(Request $request)
    {
        $tmp_id = $request->post('tmp_id', 0);
        $attr_id = $request->post('attr_id', 0);
        $attr_key = $request->post('attr_key', '');
        $attr_val = $request->post('attr_val', '');
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if ($attr_id) {
            $res = TmpService::attrEdit($attr_id, $attr_key, $attr_val);
        } else {
            $res = TmpService::attrAdd($tmp_id, $attr_key, $attr_val);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //属性删除
    public function attrDel(Request $request)
    {
        $attr_id = $request->post('attr_id', 0);
        $res = TmpService::attrDel($attr_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}